#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Drawing;
using System.Windows.Forms;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Factories;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Engine.Tools;
using WaveTech.GShield.Manager.Properties;
using WaveTech.GShield.Model;
using Exception=System.Exception;

#endregion

namespace WaveTech.GShield.Manager
{
	public partial class UsagePolicies : Form
	{
		#region Constructor

		public UsagePolicies()
		{
			InitializeComponent();
			InitializeRequiredSystems();
		}

		#endregion Constructor

		#region Initialize Required Systems

		private void InitializeRequiredSystems()
		{
		}

		#endregion Initialize Required Systems

		#region UsagePolicies_Load

		private void UsagePolicies_Load(object sender, EventArgs e)
		{
			m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
			                                                 m_bannerImage.Height, BannerFactory.BannerStyle.WinVistaBlack,
			                                                 new Bitmap(Resources.B256x256_UsagePolicy, new Size(48, 48)),
			                                                 "Usage Policies",
			                                                 "Game Usage Policies");

			PopulateUsersLists();
			SetSelectedItem();
		}

		#endregion UsagePolicies_Load

		#region Populate Users Lists

		private void PopulateUsersLists()
		{
			cboUsageUsernames.Items.Clear();
			cboUsageUsernames.Items.AddRange(Users.GetUsersList());

			lstUsageUsers.DataSource = UsageDAO.GetAllUsagePolicyUsers();
		}

		#endregion Populate Users Lists

		#region Set Selected Item

		private void SetSelectedItem()
		{
			cboUsageMonday.SelectedIndex = 0;
			cboUsageTuesday.SelectedIndex = 0;
			cboUsageWednesday.SelectedIndex = 0;
			cboUsageThursday.SelectedIndex = 0;
			cboUsageFriday.SelectedIndex = 0;
			cboUsageSaturday.SelectedIndex = 0;
			cboUsageSunday.SelectedIndex = 0;
		}

		#endregion Set Selected Item

		#region btnDeletedSelected_Click

		private void btnDeletedSelected_Click(object sender, EventArgs e)
		{
			if (lstUsageUsers.SelectedItem != null && lstUsageUsers.SelectedItem.ToString().Length > 0)
			{
				if (
					MessageBox.Show(
						"Are your sure you want to remove the usage policy for this user " + lstUsageUsers.SelectedItem +
						" from GShield database?", "Remove Usage Policy", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) ==
					DialogResult.Yes)
				{
					if (UsageDAO.GetUsagePolicyForUser(lstUsageUsers.SelectedItem.ToString()) != null)
					{
						try
						{
							UsageDAO.Delete(lstUsageUsers.SelectedItem.ToString());
						}
						catch (Exception ex)
						{
							MessageBox.Show("There there was an error trying to delete the usage policy, " + ex.Message,
							                "Delete Usage Policy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}

						Logging.Write("The usage policy for the user " + lstUsageUsers.SelectedItem + " has been deleted.",
						              LogCategory.Usage, LogPriority.Medium, LogType.UsagePolicyDel, lstUsageUsers.SelectedItem.ToString());

						lstUsageUsers.SelectedIndex = -1;

						cboUsageUsernames.SelectedItem = "";

						cboUsageMonday.SelectedIndex = 0;
						cboUsageTuesday.SelectedIndex = 0;
						cboUsageWednesday.SelectedIndex = 0;
						cboUsageThursday.SelectedIndex = 0;
						cboUsageFriday.SelectedIndex = 0;
						cboUsageSaturday.SelectedIndex = 0;
						cboUsageSunday.SelectedIndex = 0;

						PopulateUsersLists();

						MessageBox.Show("The usage policy has been deleted.", "Delete Usage Policy", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);
					}
				}
			}
			else
			{
				MessageBox.Show("You must select a user to remove the user policy for.", "Delete Usage Policy", MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}
		}

		#endregion btnDeletedSelected_Click

		#region btnSave_Click

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (cboUsageUsernames.SelectedItem != null && cboUsageUsernames.SelectedItem.ToString().Trim().Length > 0)
			{
				if (UsageDAO.GetUsagePolicyForUser(cboUsageUsernames.SelectedItem.ToString()) != null)
				{
					if (
						MessageBox.Show(
							"Are your sure you want to update the usage policy for this user " + cboUsageUsernames.SelectedItem + "?",
							"Update Usage Policy", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
					{
						UsagePolicy p = UsageDAO.GetUsagePolicyForUser(cboUsageUsernames.SelectedItem.ToString());

						if (cboUsageMonday.SelectedItem != null && cboUsageMonday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageMonday.SelectedItem.ToString() == "No Limit")
							{
								p.Monday = null;
								p.MondayType = null;
							}
							else
							{
								p.Monday = int.Parse(cboUsageMonday.SelectedItem.ToString());
								p.MondayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Monday = null;
							p.MondayType = null;
						}

						if (cboUsageTuesday.SelectedItem != null && cboUsageTuesday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageTuesday.SelectedItem.ToString() == "No Limit")
							{
								p.Tuesday = null;
								p.TuesdayType = null;
							}
							else
							{
								p.Tuesday = int.Parse(cboUsageTuesday.SelectedItem.ToString());
								p.TuesdayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Tuesday = null;
							p.TuesdayType = null;
						}

						if (cboUsageWednesday.SelectedItem != null && cboUsageWednesday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageWednesday.SelectedItem.ToString() == "No Limit")
							{
								p.Wednesday = null;
								p.WednesdayType = null;
							}
							else
							{
								p.Wednesday = int.Parse(cboUsageWednesday.SelectedItem.ToString());
								p.WednesdayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Wednesday = null;
							p.WednesdayType = null;
						}

						if (cboUsageThursday.SelectedItem != null && cboUsageThursday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageThursday.SelectedItem.ToString() == "No Limit")
							{
								p.Thursday = null;
								p.ThursdayType = null;
							}
							else
							{
								p.Thursday = int.Parse(cboUsageThursday.SelectedItem.ToString());
								p.ThursdayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Thursday = null;
							p.ThursdayType = null;
						}

						if (cboUsageFriday.SelectedItem != null && cboUsageFriday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageFriday.SelectedItem.ToString() == "No Limit")
							{
								p.Friday = null;
								p.FridayType = null;
							}
							else
							{
								p.Friday = int.Parse(cboUsageFriday.SelectedItem.ToString());
								p.FridayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Friday = null;
							p.FridayType = null;
						}

						if (cboUsageSaturday.SelectedItem != null && cboUsageSaturday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageSaturday.SelectedItem.ToString() == "No Limit")
							{
								p.Saturday = null;
								p.SaturdayType = null;
							}
							else
							{
								p.Saturday = int.Parse(cboUsageSaturday.SelectedItem.ToString());
								p.SaturdayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Saturday = null;
							p.SaturdayType = null;
						}

						if (cboUsageSunday.SelectedItem != null && cboUsageSunday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageSunday.SelectedItem.ToString() == "No Limit")
							{
								p.Sunday = null;
								p.SundayType = null;
							}
							else
							{
								p.Sunday = int.Parse(cboUsageSunday.SelectedItem.ToString());
								p.SundayType = (int) TimeType.Minutes;
							}
						}
						else
						{
							p.Sunday = null;
							p.SundayType = null;
						}

						UsageDAO.Insert(p);

						Logging.Write("The usage policy for the user " + lstUsageUsers.SelectedItem + " has been updated.",
						              LogCategory.Usage, LogPriority.Medium, LogType.UsagePolicyUp, lstUsageUsers.SelectedItem.ToString());

						PopulateUsersLists();

						MessageBox.Show("The usage policy has been updated.", "Update Usage Policy", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);
					}
				}
				else
				{
					if (
						MessageBox.Show(
							"Are your sure you want to add the usage policy for this user " + cboUsageUsernames.SelectedItem + "?",
							"Update Usage Policy", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
					{
						UsagePolicy p = new UsagePolicy();

						p.Username = cboUsageUsernames.SelectedItem.ToString();

						if (cboUsageMonday.SelectedItem != null && cboUsageMonday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageMonday.SelectedItem.ToString() == "No Limit")
							{
								p.Monday = null;
								p.MondayType = null;
							}
							else
							{
								p.Monday = int.Parse(cboUsageMonday.SelectedItem.ToString());
								p.MondayType = (int) TimeType.Minutes;
							}
						}

						if (cboUsageTuesday.SelectedItem != null && cboUsageTuesday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageTuesday.SelectedItem.ToString() == "No Limit")
							{
								p.Tuesday = null;
								p.TuesdayType = null;
							}
							else
							{
								p.Tuesday = int.Parse(cboUsageTuesday.SelectedItem.ToString());
								p.TuesdayType = (int) TimeType.Minutes;
							}
						}

						if (cboUsageWednesday.SelectedItem != null && cboUsageWednesday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageWednesday.SelectedItem.ToString() == "No Limit")
							{
								p.Wednesday = null;
								p.WednesdayType = null;
							}
							else
							{
								p.Wednesday = int.Parse(cboUsageWednesday.SelectedItem.ToString());
								p.WednesdayType = (int) TimeType.Minutes;
							}
						}

						if (cboUsageThursday.SelectedItem != null && cboUsageThursday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageThursday.SelectedItem.ToString() == "No Limit")
							{
								p.Thursday = null;
								p.ThursdayType = null;
							}
							else
							{
								p.Thursday = int.Parse(cboUsageThursday.SelectedItem.ToString());
								p.ThursdayType = (int) TimeType.Minutes;
							}
						}

						if (cboUsageFriday.SelectedItem != null && cboUsageFriday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageFriday.SelectedItem.ToString() == "No Limit")
							{
								p.Friday = null;
								p.FridayType = null;
							}
							else
							{
								p.Friday = int.Parse(cboUsageFriday.SelectedItem.ToString());
								p.FridayType = (int) TimeType.Minutes;
							}
						}

						if (cboUsageSaturday.SelectedItem != null && cboUsageSaturday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageSaturday.SelectedItem.ToString() == "No Limit")
							{
								p.Saturday = null;
								p.SaturdayType = null;
							}
							else
							{
								p.Saturday = int.Parse(cboUsageSaturday.SelectedItem.ToString());
								p.SaturdayType = (int) TimeType.Minutes;
							}
						}

						if (cboUsageSunday.SelectedItem != null && cboUsageSunday.SelectedItem.ToString().Trim().Length > 0)
						{
							if (cboUsageSunday.SelectedItem.ToString() == "No Limit")
							{
								p.Sunday = null;
								p.SundayType = null;
							}
							else
							{
								p.Sunday = int.Parse(cboUsageSunday.SelectedItem.ToString());
								p.SundayType = (int) TimeType.Minutes;
							}
						}

						UsageDAO.Update(p);

						Logging.Write("A new usage policy for the user " + p.Username + " has been created.", LogCategory.Usage,
						              LogPriority.Medium, LogType.UsagePolicyAdd, p.Username);

						PopulateUsersLists();

						MessageBox.Show("The usage policy has been created.", "Create Usage Policy", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);
					}
				}
			}
		}

		#endregion btnSave_Click

		#region btnClose_Click

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		#endregion btnClose_Click

		#region lstUsageUsers_SelectedIndexChanged

		private void lstUsageUsers_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstUsageUsers.SelectedItem != null)
			{
				UsagePolicy p = UsageDAO.GetUsagePolicyForUser(lstUsageUsers.SelectedItem.ToString());

				if (p != null)
				{
					cboUsageUsernames.SelectedItem = p.Username;

					if (p.Monday.HasValue)
						if (p.Monday.Value == 0)
							cboUsageMonday.SelectedIndex = 1;
						else
							cboUsageMonday.SelectedItem = p.Monday.Value.ToString();
					else
						cboUsageMonday.SelectedIndex = 0;

					if (p.Tuesday.HasValue)
						if (p.Tuesday.Value == 0)
							cboUsageTuesday.SelectedIndex = 1;
						else
							cboUsageTuesday.SelectedItem = p.Tuesday.Value.ToString();
					else
						cboUsageTuesday.SelectedIndex = 0;

					if (p.Wednesday.HasValue)
						if (p.Wednesday.Value == 0)
							cboUsageWednesday.SelectedIndex = 1;
						else
							cboUsageWednesday.SelectedItem = p.Wednesday.Value.ToString();
					else
						cboUsageWednesday.SelectedIndex = 0;

					if (p.Thursday.HasValue)
						if (p.Thursday.Value == 0)
							cboUsageThursday.SelectedIndex = 1;
						else
							cboUsageThursday.SelectedItem = p.Thursday.Value.ToString();
					else
						cboUsageThursday.SelectedIndex = 0;

					if (p.Friday.HasValue)
						if (p.Friday.Value == 0)
							cboUsageFriday.SelectedIndex = 1;
						else
							cboUsageFriday.SelectedItem = p.Friday.Value.ToString();
					else
						cboUsageFriday.SelectedIndex = 0;

					if (p.Saturday.HasValue)
						if (p.Saturday.Value == 0)
							cboUsageSaturday.SelectedIndex = 1;
						else
							cboUsageSaturday.SelectedItem = p.Saturday.Value.ToString();
					else
						cboUsageSaturday.SelectedIndex = 0;

					if (p.Sunday.HasValue)
						if (p.Sunday.Value == 0)
							cboUsageSunday.SelectedIndex = 1;
						else
							cboUsageSunday.SelectedItem = p.Sunday.Value.ToString();
					else
						cboUsageSunday.SelectedIndex = 0;
				}
			}
			else
			{
				cboUsageUsernames.SelectedIndex = -1;

				cboUsageMonday.SelectedIndex = 0;
				cboUsageTuesday.SelectedIndex = 0;
				cboUsageWednesday.SelectedIndex = 0;
				cboUsageThursday.SelectedIndex = 0;
				cboUsageFriday.SelectedIndex = 0;
				cboUsageSaturday.SelectedIndex = 0;
				cboUsageSunday.SelectedIndex = 0;
			}
		}

		#endregion lstUsageUsers_SelectedIndexChanged

		#region btnHelp_Click

		private void btnHelp_Click(object sender, EventArgs e)
		{
			Help.ShowHelp(this, Settings.Default.HelpFileName, HelpNavigator.TopicId, Settings.Default.HelpTopics_UsagePolicyForm);
		}

		#endregion btnHelp_Click

		private void cboUsageTuesday_SelectedIndexChanged(object sender, EventArgs e)
		{
		}
	}
}